package top.jiangqiang.qyblog.admin.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import top.jiangqiang.qyblog.common.base.controller.BaseController;
import top.jiangqiang.qyblog.common.base.query.PageResult;
import top.jiangqiang.qyblog.common.base.response.Result;
import top.jiangqiang.qyblog.common.constant.RequestURLConstants;
import top.jiangqiang.qyblog.core.constant.StorageTypeEnum;
import top.jiangqiang.qyblog.core.domain.dto.StorageAddDto;
import top.jiangqiang.qyblog.core.domain.dto.StorageQueryDto;
import top.jiangqiang.qyblog.core.domain.dto.StorageUpdateDto;
import top.jiangqiang.qyblog.core.domain.vo.StorageVo;
import top.jiangqiang.qyblog.core.service.IStorageService;

/**
 * @author jiangqiang
 * @date 2022-12-15
 */
@RequiredArgsConstructor
@RestController
@RequestMapping(RequestURLConstants.STORAGE_CONTROLLER_PREFIX)
@Tag(name = "存储器管理接口")
public class StorageController extends BaseController {
    private final IStorageService storageService;

    @GetMapping
    @Operation(summary = "根据条件查询存储器列表", description = "支持分页，排序等功能")
    public Result<PageResult<StorageVo>> list(@ParameterObject StorageQueryDto query) {
        return Result.ok(storageService.findByCondition(query));
    }

    @GetMapping("/types")
    @Operation(summary = "获取支持的存储器类型", description = "获取所有系统支持的存储器类型列表")
    public Result<StorageTypeEnum[]> types() {
        //这里需要对枚举类型使用注解配置，否则会根据枚举对象名字序列化
        //@JsonFormat(shape = JsonFormat.Shape.OBJECT)
        return storageService.getTypes();
    }

    @PostMapping
    @Operation(summary = "新增存储器", description = "新增一个存储器，新增时会校验各项参数是否正确，新增后无法删除，并且默认不会激活")
    public Result<String> save(@RequestBody StorageAddDto storageAddDto) {
        return storageService.addStorage(storageAddDto);
    }

    @PutMapping
    @Operation(summary = "编辑存储器", description = "根据id修改一个存储器，只允许修改存储器名，其他配置不可修改")
    public Result<String> update(@RequestBody StorageUpdateDto storageUpdateDto) {
        return storageService.editStorage(storageUpdateDto);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除存储器", description = "根据id删除指定存储器，如果存储器已经被使用，则无法删除")
    public Result<Boolean> delete(@PathVariable Long id) {
        return storageService.deleteStorage(id);
    }

    @PostMapping("/active/{id}")
    @Operation(summary = "激活存储器", description = "新增或修改存储器不会影响存储器状态，只有调用此接口激活后才会真正生效。" +
            "激活某一个存储器时会自动失效其他存储器。如果此时正在上传文件，请不要随意修改激活的存储器，否则上传会失败")
    public Result<String> active(@PathVariable @NotNull(message = "id不能为空") Long id) {
        return storageService.activeStorage(id);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询存储策略详情", description = "根据id查询策略详情")
    public Result<StorageVo> get(@PathVariable Long id) {
        return storageService.getStorageDetail(id);
    }
}
